<?php
namespace app\oem\controller;
use think\Controller;
use think\Db;
use app\common\controller\OemBaseController;
use app\common\model\Csv;

class Accounts extends OemBaseController
{
 	protected $csv;
    protected function _initialize() {
        parent::_initialize();
        $this->csv = new Csv();
    }
    
	//帐号管理
	public function user()
    {
    	$user_level=session("user_level");
		if($user_level==1){//oem
			$where['oem_id']=session('d_id');
		}elseif($user_level==2){//省代理
			$where['agency_id1']=session('d_id');
		}elseif($user_level==3){
			$where['agency_id2']=session('d_id');
		}
		if(input("mobile")){
			$where['mobile']=["like","%".input("mobile")."%"];
			$this->assign("mobile",input("mobile"));
		}else{ 
			$this->assign("mobile",'');
		}
    	$list=db('info_user')->where($where)->order('add_time desc')->paginate(10);
		$this->assign("list",$list);
		$this->assign('empty','<span class="empty">没有数据</span>');
    	return $this->fetch();
    }
    //会员改变状态
	public function user_status()
    {
    	$where['status'] = input('status');
    	$ress = db('info_user')->where('id',input('id'))->update($where);
    	if($ress){
			return json(['code'=>200,'msg'=>'成功']);
		}else{
			return json(['code'=>402,'msg'=>'失败']);
		}
    }
    public function agent()
    {
    	$user_level=session("user_level");
		if($user_level==1){//oem
			$where['oem_id']=session('d_id');
		}elseif($user_level==2){//省代理
			$where['agency_id1']=session('d_id');
		}elseif($user_level==3){
			$where['agency_id2']=session('d_id');
		}
		if(input('agent_name')){
			$query['agent_name'] = input('agent_name');
			$this->assign("agent_name",input("agent_name"));
			$where['agent_name'] = ['like', '%'.input('agent_name').'%'];
		}else{
			$this->assign("agent_name",'');
		}
		if(input('mobile')){
			$query['mobile'] = input('mobile');
			$this->assign("mobile",input("mobile"));
			$where['mobile'] = input('mobile');
		}else{ 
			$this->assign("mobile",'');
		}
    	$list=db('info_agent')->where($where)->paginate(10);
		$this->assign("list",$list);
		$this->assign('empty','<span class="empty">没有数据</span>');
    	return $this->fetch();
    }
	public function show_agent(){
		$account=db('info_agent')->find(input("id"));
		$this->assign('ad',$account);
		return $this->fetch();
	}
	//修改商家费率
	public function upagent_huilv()
    {
    	if(request()->isPost()){
	    	$where['temp_fee'] = input('cb_huilu');
			$where['is_huilu'] = 2;
	    	$ress = db('info_agent')->where('id',input('id'))->update($where);
	    	if($ress){
				return json(['code'=>200,'msg'=>'提交成功等待通过']);
			}else{
				return json(['code'=>402,'msg'=>'提交失败']);
			}
    	}
    	$this->assign("id",input('id'));
    	return $this->fetch();
    }
    public function agency_id1()
    {
    	$user_level=session("user_level");
		if($user_level==1){//oem
			$where['oem_id']=session('d_id');
		}
		$where['user_level']=2;
    	$list=db('info_oem')->where($where)->select();
		$this->assign("list",$list);
		$this->assign('empty','<span class="empty">没有数据</span>');
    	return $this->fetch();
    }
    public function agency_id2()
    {
		$user_level=session("user_level");
		if($user_level==1){//oem
			$where['m.oem_id']=session('d_id');
		}elseif($user_level==2){//oem
			$where['m.agency_id']=session('d_id');
		}
		$where['m.user_level']=3;
		$field = 'm.*,t.user_name as tname';
    	$list = Db::name("info_oem")->alias('m')->
		join('info_oem t','t.id = m.agency_id','LEFT')->
		field($field)->where($where)->order('m.add_time desc')->paginate(10);
    	
		$this->assign("list",$list);
		$this->assign('empty','<span class="empty">没有数据</span>');
    	return $this->fetch();
    }
    //业务员
    public function salesman()
    {
    	$user_level=session("user_level");
		if($user_level==1){//oem
			$where['oem_id']=session('d_id');
		}elseif($user_level==2){//省代理
			$where['agency_id1']=session('d_id');
		}elseif($user_level==3){
			$where['agency_id2']=session('d_id');
		}
    	$list=db('info_salesman')->where($where)->select();
		$this->assign("list",$list);
		$this->assign('empty','<span class="empty">没有数据</span>');
    	return $this->fetch();
    }
	//添加商家
	public function add_agent(){
		if(request()->isPost()){
			$is_reg=db('info_agent')->where("mobile",input("mobile"))->find();
			if($is_reg){
				return json(['code'=>402,'msg'=>'添加失败,该手机号码已经添加过']);
			}
			$cut_infor_oem = db('info_oem')->where("id",session('d_id'))->find();
			
			$is_userreg=db('info_user')->where("mobile",input("mobile"))->find();
			if($is_userreg){
				//已经存在会员信息
				$user_id= $is_userreg['id'];
				$where['oem_id'] 	 = $cut_infor_oem['oem_id'];
				$where['agency_id1'] = $cut_infor_oem['city_id'];
				$where['agency_id2'] = $cut_infor_oem['id'];
	    		$ress = db('info_user')->where('id',$is_userreg['id'])->update($where);
			}else{
				//同时添加会员
				$datas['mobile'] 	 = input("mobile");
				$datas['password'] 	 = md5_salt("123456");
				$datas['oem_id'] 	 = $cut_infor_oem['oem_id'];
				$datas['agency_id1'] = $cut_infor_oem['city_id'];
				$datas['agency_id2'] = $cut_infor_oem['id'];
				$datas['add_time']	 =now_time();
				db('info_user')->insert($datas);
				$user_id= Db::name('info_user')->getLastInsID();
			}
									
			$param['user_id'] 	= $user_id;
			$param['add_time'] 	= now_time();
			$param['mobile'] 	= input("mobile");
			$param['agent_name'] 	= input("agent_name");
			$param['user_name'] 	= input("user_name");
			$param['logo']	=input("thumb");
			$param['store_property'] 	= input("store_property");
			$param['password'] 	= md5_salt("123456");
			$param['operation_pwd'] 	= md5_salt("123456");
			$user_level=session("user_level");
			$param['oem_id']	=$cut_infor_oem['oem_id'];
			$param['agency_id1']=$cut_infor_oem['city_id'];
			$param['agency_id2']=$cut_infor_oem['id'];
			$res = db('info_agent')->insert($param);
			if($res){
				$agent_id= Db::name('info_agent')->getLastInsID();
				$data['agent_id']=$agent_id;
				$data['oem_id']=session('oem_id');
				$data['store_name']=input("agent_name");
				$data['add_time']=now_time();
				db('agent_store')->insert($data);				
				return json(['code'=>200,'msg'=>'添加成功','data'=>$param]);
			}else{
				return json(['code'=>402,'msg'=>'添加失败']);
			}
    	}
    	return $this->fetch();
	}
	//添加省代
    public function add_agency(){
    	if(request()->isPost()){
			$is_reg=db('info_oem')->where("mobile",input("mobile"))->find();
			if($is_reg){
				return json(['code'=>402,'msg'=>'添加失败,该手机号码已经添加过']);
			}
			$param['add_time'] 		= now_time();
			$param['mobile'] 		= input("mobile");
			$param['province'] 		= input("province");
			$param['user_name'] 	= input("user_name");
			$param['password'] 		= md5_salt(input("password"));
			$param['operation_pwd'] = md5_salt(input("operation_pwd"));
			$param['status'] 		= input("status");
			$param['user_level']  	= 2;
			$param['oem_id']=session('d_id');
		
			$res = db('info_oem')->insert($param);
			if($res){
				return json(['code'=>200,'msg'=>'添加成功','data'=>$param]);
			}else{
				return json(['code'=>402,'msg'=>'添加失败']);
			}
			
    	}
    	return $this->fetch();
    }
    //添加业务员
    public function add_manager(){
    	if(request()->isPost()){
			$is_reg=db('info_salesman')->where("mobile",input("mobile"))->find();
			if($is_reg){
				return json(['code'=>402,'msg'=>'添加失败,该手机号码已经添加过']);
			}
			
			$cut_infor_oem = db('info_oem')->where("id",session('d_id'))->find();
			$param['add_time'] 		= now_time();
			$param['mobile'] 		= input("mobile");
			$param['name'] 			= input("name");
			$param['oem_id']		= $cut_infor_oem['oem_id'];
			$param['agency_id1']	= $cut_infor_oem['agency_id'];
			$param['agency_id2']	= $cut_infor_oem['id'];
			$res = db('info_salesman')->insert($param);
			if($res){
				return json(['code'=>200,'msg'=>'添加成功','data'=>$param]);
			}else{
				return json(['code'=>402,'msg'=>'添加失败']);
			}
			
    	}
    	return $this->fetch();
    }
    public function edit_agency(){
		if(request()->isPost()){
			$param['add_time'] 	= now_time();
			$param['mobile'] 	= input("mobile");
			$param['user_name'] 	= input("user_name");
			if(input("password")){
				$param['password'] 	= md5_salt(input("password"));
			}
			if(input("operation_pwd")){
				$param['operation_pwd'] 	= md5_salt(input("operation_pwd"));
			}
			$param['status'] 	= input("status");
			$param['id']=input("id");
			$res = db('info_oem')->update($param);
			if($res){
				return json(['code'=>200,'msg'=>'修改成功','data'=>$param]);
			}else{
				return json(['code'=>402,'msg'=>'修改失败']);
			}
    	}
		$account=db('info_oem')->find(input("id"));
		$this->assign('ad',$account);
		return $this->fetch();
	}
	//添加市代
	public function add_agency2(){
		if(request()->isPost()){
			$is_reg=db('info_oem')->where("mobile",input("mobile"))->find();
			if($is_reg){
				return json(['code'=>402,'msg'=>'添加失败,该手机号码已经添加过']);
			}
			
			$cut_infor_oem = db('info_oem')->where("id",session('d_id'))->find();
			$param['add_time'] 		= now_time();
			$param['mobile'] 		= input("mobile");
			$param['province'] 		= input("province");
			$param['city'] 			= input("city");
			$param['user_name'] 	= input("user_name");
			$param['addres'] 		= input("addres");
			$param['password'] 		= md5_salt(input("password"));
			$param['operation_pwd'] = md5_salt(input("operation_pwd"));
			$param['status'] 		= input("status");
			$param['user_level']  	= 3;
			$param['oem_id']		=$cut_infor_oem['oem_id'];
			$param['agency_id']		=$cut_infor_oem['id'];
		
			$res = db('info_oem')->insert($param);
			if($res){
				return json(['code'=>200,'msg'=>'添加成功','data'=>$param]);
			}else{
				return json(['code'=>402,'msg'=>'添加失败']);
			}
			
    	}
    	return $this->fetch();
	}
	
	//联盟商圈审核
	public function union_sh(){
		
		if(request()->isPost()){
    		$param=input('post.');
			if(!empty($param['store_name']))$where['a.store_name'] = ['like','%'.$param['store_name'].'%'];
			if(!empty($param['state']))$where['a.state'] = ['like','%'.$param['state'].'%'];
    	}
    	$where['a.oem_id'] = session('d_id');
    	$list = Db::table('union_agent')
    	->alias('a')
		->join('info_user w','a.u_id = w.id')
		->field('a.*,w.mobile')
    	->where($where)
    	->order('a.add_time desc')
    	->paginate(5);
        $this->assign('list',$list);
        $this->assign('empty','<span class="empty">没有数据</span>');
    	return $this->fetch();
	}
	
	//联盟商圈审核
	public function union_shenhe(){
    	$param['state'] 	= input("lid");
    	$list = Db::table('union_agent')->where('id',input("id"))->update($param);
    	if($list){
			return json(['code'=>200,'msg'=>'成功']);
		}else{
			return json(['code'=>402,'msg'=>'失败']);
		}
	}
	//业务员推广二维码
	public function tuiguang(){
    	$data = Db::table('info_salesman')->where('id',input("id"))->find();
		//二维码链接
    	$redirect_url="http://".$_SERVER['HTTP_HOST']."/index/index/regist?fromuid=1".
    	"&oem_id=".$data['oem_id']."&salesman_id=".$data['id'];
		$data['redirect_url'] = $redirect_url;
    	
    	$this->assign('data',$data);
    	return $this->fetch();
	}
	
	
    //商家导出
    public function agent_dcxml(){
	 	$param = input('post.');
		$query=[];
		//商家
		if(input('agent_name')){
			$query['a.agent_name'] = input('agent_name');
			$this->assign("agent_name",input("agent_name"));
			$map['a.agent_name'] = ['like', '%'.input('agent_name').'%'];
		}else{
			$this->assign("agent_name",'');
		}
		if(input('mobile')){
			$query['a.mobile'] = input('mobile');
			$this->assign("mobile",input("mobile"));
			$map['a.mobile'] = input('mobile');
		}else{ 
			$this->assign("mobile",'');
		}
		if(session('user_level')==1){
			$map['a.oem_id'] = session('d_id');
		}elseif(session('user_level')==2){
			$map['a.agency_id1'] = session('d_id');
		}else{
			$map['a.agency_id2'] = session('d_id');
		}
		$list=db("info_agent")
				->alias('a')
				->join('agent_qianyue b ','a.id= b.agent_id')
				->where($map)
				->order('a.add_time desc')
				->select(); 
		$str ="店铺名称,姓名,手机号码,联盟商圈,创建时间,"."\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($list as $k=>$row){
        	$agent_name = iconv('utf-8','gbk',$row['agent_name']);
			$tel = iconv('utf-8','gbk',$row['mobile']);
			$name = iconv('utf-8','gbk',$row['contacts']);
			//$sex = iconv('utf-8','gbk',$row['sex']);
			//$nick_name = utf8ToGbk($row['nick_name']);
			if($row['is_union']==1){
				$payname = '启用';
			}else {
				$payname = '不启用';
			}
			$typename = iconv('utf-8','gbk',$payname); 
			$add_time = iconv('utf-8','gbk','	'.$row['add_time']);
			$str .= $agent_name.",".$name.",".$tel.",".$typename.",".$add_time.","."\n"; //用引文逗号分开 
        } 

	  	$filename ='商家列表.csv';   //设置文件名  
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
	}
	//会员导出
    public function user_dcxml(){
	 	$param = input('post.');
		$query=[];
		if(input('mobile')){
			$query['mobile'] = input('mobile');
			$this->assign("mobile",input("mobile"));
			$map['mobile'] = input('mobile');
		}else{
			$this->assign("mobile",'');
		}
		if(session('user_level')==1){
			$map['oem_id'] = session('d_id');
		}elseif(session('user_level')==2){
			$map['agency_id1'] = session('d_id');
		}else{
			$map['agency_id2'] = session('d_id');
		}
		$list=db("info_user")->where($map)->order('add_time desc')->select(); 
		$str ="手机号,会员名,状态,注册时间,"."\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($list as $k=>$row){
        	$id = iconv('utf-8','gbk',$row['id']);
			$mobile = iconv('utf-8','gbk',$row['mobile']);
			$user_name = iconv('utf-8','gbk',$row['user_name']);
			if($row['status']==1){
				$payname = '正常';
			}else {
				$payname = '冻结 ';
			}
			$typename = iconv('utf-8','gbk',$payname); 
			$add_time = iconv('utf-8','gbk','	'.$row['add_time']);
			$str .= $mobile.",".$user_name.",".$typename.",".$add_time.","."\n"; //用引文逗号分开 
        } 

	  	$filename ='会员列表.csv';   //设置文件名  
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
	}
	
    public function show_user(){
		$account=db('info_user')->find(input("id"));
		$this->assign('ad',$account);
		return $this->fetch();
	}
    
}
